package com.lx.controller;

import com.lx.config.CheckUserAndPwd;

import javafx.application.Application;

import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.fxml.JavaFXBuilderFactory;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;

/**view展示界面，model做业务处理，相当于dao(CRUD) service(事务CRUD)
 * Controller不做任何处理，只调用方法
 *
 *我这里做了业务处理
 *从登录页面开始跳转
 * ***/
public class MainApp extends Application {

    private Stage stage;
    private final double MINIMUM_WINDOW_WIDTH = 700.0;
    private final double MINIMUM_WINDOW_HEIGHT = 550.0;

    public void start(Stage primaryStage) throws Exception {

        stage = primaryStage;
        primaryStage.setTitle("拼图");


        // 标题栏图标
        Image image = new Image("/img/icon.png");
        primaryStage.getIcons().add(image);

        //网格布局
        GridPane grid = new GridPane();
        grid.setAlignment(Pos.CENTER);
        grid.setHgap(10);
        grid.setVgap(10);


        grid.setPadding(new Insets(25, 25, 25, 25));

        Scene scene = new Scene(grid, 750, 500);

        primaryStage.setScene(scene);

        scene.getStylesheets().add
                (MainApp.class.getResource("/css/login.css").toExternalForm());


        Text scenetitle = new Text("Welcome");
        //新添加
        scenetitle.setId("welcome-text");

        //修改为内联样式
        //scenetitle.setFont(Font.font("Tahoma", FontWeight.NORMAL, 20));
        grid.add(scenetitle, 0, 0, 2, 1);

        Label userName = new Label("userName:");
        grid.add(userName, 0, 1);
        TextField userTextField = new TextField();
        grid.add(userTextField, 1, 1);

        Label pw = new Label("passWord:");
        grid.add(pw, 0, 2);
        PasswordField pwBox = new PasswordField();
        grid.add(pwBox, 1, 2);
        //grid.getChildren().add(pwBox);

        //单选框
        RadioButton dx = new RadioButton("记住密码");
        HBox dxBtn = new HBox(5);
        dxBtn.setAlignment(Pos.BOTTOM_LEFT);
        dxBtn.getChildren().add(dx);
        grid.add(dxBtn, 1, 3);

        //超链接忘记密码
        Hyperlink link = new Hyperlink("忘记密码");
        HBox linkBtn = new HBox(5);
        linkBtn.setAlignment(Pos.BOTTOM_RIGHT);
        linkBtn.getChildren().add(link);
        grid.add(linkBtn, 2, 3);

        final WebView browser = new WebView();
        final WebEngine webEngine = browser.getEngine();
        link.setOnAction((ActionEvent e) -> {

            //加载注册界面

        });


        //注册
       Button zc = new Button("注册");
        HBox zcBtn = new HBox(5);
        zcBtn.setAlignment(Pos.BOTTOM_LEFT);
        zcBtn.getChildren().add(zc);
        grid.add(zcBtn, 1, 4);


        Button dl = new Button("登录");
        HBox dlBtn = new HBox(5);
        dlBtn.setAlignment(Pos.BOTTOM_RIGHT);
        dlBtn.getChildren().add(dl);
        grid.add(dlBtn, 2, 4);


        //文本
        final Text actiontarget = new Text();
        grid.add(actiontarget, 1, 6);


        primaryStage.show();

        //加载事件监听注册界面 换一种setOnAction
        zc.setOnAction(new EventHandler<ActionEvent>() {

            public void handle(ActionEvent e) {

                gotoRegist();
            }
        });

        //登录鼠标监听
        dl.setOnMouseClicked(e -> {
            String name = userTextField.getText();
            String password = pwBox.getText();
            if ("admin".equals(name) && "admin".equals(password)) {
                System.out.println("登录成功");
                gotomain();//跳转到主页面需要鼠标监听





            }/*else if(name.isEmpty()&&"".equals(password)){
                System.out.println("登录失败");
                // mainController.gotoLogin();//跳转到登录界面
                dl.setOnAction(new EventHandler<ActionEvent>() {

                    @Override
                    public void handle(ActionEvent e) {
                        //grid.add(actiontarget, 1, 6);

                        // actiontarget.setId("welcome-text");
                        actiontarget.setFill(Color.FIREBRICK);
                        actiontarget.setText("用户名或者密码为空");
                    }
                });
            }*/else {
                System.out.println("登录失败");
                // mainController.gotoLogin();//跳转到登录界面
                dl.setOnAction(new EventHandler<ActionEvent>() {

                    @Override
                    public void handle(ActionEvent e) {
                        grid.add(actiontarget, 1, 6);

                        // actiontarget.setId("welcome-text");
                        actiontarget.setFill(Color.FIREBRICK);
                        actiontarget.setText("用户名或者密码错误");
                    }
                });
            }
        });



    }

//跳转到登录界面
    public void gotologin(){
        try {
            LoginController login = (LoginController) replaceSceneContent("/login.fxml");
            login.setApp(this);
        } catch (Exception ex) {
            Logger.getLogger(MainApp.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    //跳转到注册界面
    public void gotoRegist(){

        try {
            RegistController regist = (RegistController)  replaceSceneContent("/regist.fxml");
            regist.setApp(this);
        } catch (Exception ex) {
            Logger.getLogger(MainApp.class.getName()).log(Level.SEVERE, null, ex);
        }
    }


    //跳转到主界面
    public void gotomain(){
        try {
            MainController main = (MainController) replaceSceneContent("/main.fxml");
            main.setApp(this);
        } catch (Exception ex) {
            Logger.getLogger(MainApp.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public void userlogin(String user,String password){
        if(CheckUserAndPwd.checkReturn(user,password)){
            gotomain();
        }
    }
    //退出注册界面
    public void backregist(){

        gotologin();
    }
    //退出主界面
    public void outmain(){
        gotologin();
    }
    private Initializable replaceSceneContent(String fxml) throws Exception {
        FXMLLoader loader = new FXMLLoader();
        InputStream in = MainApp.class.getResourceAsStream(fxml);
        loader.setBuilderFactory(new JavaFXBuilderFactory());
        loader.setLocation(MainApp.class.getResource(fxml));
        AnchorPane page;
        try {
            page = (AnchorPane) loader.load(in);
        } finally {
            in.close();
        }
        Scene scene = new Scene(page, 800, 600);
        stage.setScene(scene);
        stage.sizeToScene();
        return (Initializable) loader.getController();
    }
}
